package com.shupian.controller;

import com.shupian.domain.ResponseResult;
import com.shupian.domain.entity.Menu;
import com.shupian.enums.AppHttpCodeEnum;
import com.shupian.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Min;
import javax.xml.ws.Action;
import java.util.List;

@RestController
@RequestMapping("/system/menu")
@Validated
public class MenuController {

    @Autowired
    private MenuService menuService;

    /**
     * 查询菜单列表
     * @return
     */

    @GetMapping("/list")
    public ResponseResult getAllMenu(){
        return menuService.getAllMenu();
    }

    @PostMapping
    public ResponseResult createMenu(@RequestBody Menu menu) {
        return menuService.save(menu) ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
    }

    @GetMapping("/{id}")
    public ResponseResult getMenuById(@PathVariable("id") @Min(1) Integer id){
        return menuService.getMenuById(id);
    }
    @PutMapping
    public ResponseResult updateMenu(@RequestBody  Menu menu){
        return menuService.updateById(menu) ? ResponseResult.okResult():ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
    }
    @DeleteMapping("/{menuId}")
    public ResponseResult deleteById(@PathVariable("menuId") @Min(1) Integer id){
        return menuService.removeById(id) ? ResponseResult.okResult():
        ResponseResult.errorResult(AppHttpCodeEnum.DELETE_ERROR);
    }

    /**
     * 加载对应角色菜单列表树接口
     * @param id
     * @return
     */
    @GetMapping("/roleMenuTreeselect/{id}")
    public ResponseResult roleMenuTreeselect(@PathVariable("id") Long id){
        return menuService.roleMenuTreeselect(id) ;
    }





}
